﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Synchronization.Validation.Internals;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Common
{
    public class RulesList 
    {
        private readonly List<RuleMetadata> allRules = new List<RuleMetadata>();

        public RulesList()
        {
            var assembly = typeof(Rule<>).Assembly;

            var types = assembly.GetTypes().Where(t => t.IsSubclassOfRawGeneric(typeof(Rule<>)));

            foreach (var type in types)
            {
                allRules.Add((RuleMetadata)Activator.CreateInstance(type, null));
            }
        }

        public List<RuleMetadata> GetAllRules()
        {
            return allRules;
        }

        public List<RuleMetadata> GetRulesBySeverity(Severity severity)
        {
            return allRules.Where(x => x.Severity == severity).OrderBy(x => x.Code, new AlphanumericComparator()).ToList();
        }

        public List<RuleMetadata> GetRulesByBusinessResearchProduct(BusinessResearchProduct research)
        {
            return allRules.Where(x => x.BusinessResearchProduct == research)
                .OrderBy(x => x.Code, new AlphanumericComparator()).ToList();
        }

    }
}